package mianyang.zhangsan.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.mapper.LeaveWordMapper;
import mianyang.zhangsan.com.pojo.LeaveWord;
import mianyang.zhangsan.com.service.LeaveWordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("leaveWordService")
public class LeaveWordServiceImpl implements LeaveWordService {
    @Autowired
    private LeaveWordMapper leaveWordMapper;

    /**
     * 分页查询所有留言信息
     * @param pageNo
     * @return
     */
    @Override
    public Page<LeaveWord> queryPage(int pageNo) {
        Page<LeaveWord> leaveWordPage = leaveWordMapper.selectPage(new Page<>(pageNo, 3), null);
        return leaveWordPage;
    }

    /**
     * 添加一条留言
     * @param leaveWord
     */
    @Override
    public void leaveAdd(LeaveWord leaveWord) {
        leaveWordMapper.insert(leaveWord);
    }

    /**
     * 按条件分页查询留言信息
     * @param page
     * @param rows
     * @param leaveWord
     * @return
     */
    @Override
    public Page<LeaveWord> queryPageWrapper(int page, int rows, LeaveWord leaveWord) {
        QueryWrapper<LeaveWord> wrapper = queryWrapper(leaveWord);
        return leaveWordMapper.selectPage(new Page<>(page, rows), wrapper);
    }

    /**
     * 删除一条留言
     * @param leaveId
     */
    @Override
    public void delete(String leaveId) {
        int id = Integer.parseInt(leaveId);
        QueryWrapper<LeaveWord> wrapper = new QueryWrapper<>();
        wrapper.eq("leavewordid", id);
        leaveWordMapper.delete(wrapper);
    }

    /**
     * 删除多条留言
     * @param leaveIds
     */
    @Override
    public void deletes(String leaveIds) {
        String[] leaves = leaveIds.split(",");
        for (String leave:leaves){
            delete(leave);
        }
    }

    /**
     * 更新一条留言
     * @param leaveId
     * @param leaveWord
     */
    @Override
    public void update(int leaveId, LeaveWord leaveWord) {
        QueryWrapper<LeaveWord> wrapper = new QueryWrapper<>();
        wrapper.eq("leavewordid", leaveId);
        leaveWordMapper.update(leaveWord, wrapper);
    }

    /**
     * 根据条件查询所有留言
     * @param leaveWord
     * @return
     */
    @Override
    public List<LeaveWord> queryListByWrapper(LeaveWord leaveWord) {
        QueryWrapper<LeaveWord> wrapper = queryWrapper(leaveWord);
        return leaveWordMapper.selectList(wrapper);
    }

    /**
     * 封装查询条件
     * @param leaveWord
     * @return
     */
    @Override
    public QueryWrapper<LeaveWord> queryWrapper(LeaveWord leaveWord) {
        QueryWrapper<LeaveWord> wrapper = new QueryWrapper<>();
        if (leaveWord.getTitle() != null && !"".equals(leaveWord.getTitle())){
            wrapper.like("leavetitle", leaveWord.getTitle());
        }
        if (leaveWord.getUsername() != null && !"".equals(leaveWord.getUsername())){
            wrapper.eq("userobj", leaveWord.getUsername());
        }
        if (leaveWord.getTime() != null && !"".equals(leaveWord.getTime())){
            wrapper.le("leavetime", leaveWord.getTime());
        }
        return wrapper;
    }
}
